﻿define(function(require,exports,module) {
	var myPub = require('./public');

    var oWindow = $(window);
    var oHeader = $('.js_headerBox');               // 头部
    var oFooter = $('.js_footer');                  // 底部
    var oContain = $('.js_contain');                // 页面内容区域

    var resizeTimer = null;
	var oGift_contain = $('.js_gift_contain');      // 送礼页面主体

    var oCountDownPart = $('.js_countDownPart');    // 倒计时区域
    var giftHour = $('.js_countHour'); 			    // 倒计时的小时
    var giftMinute = $('.js_countMinute'); 		    // 倒计时的分钟
    var giftSecond = $('.js_countSecond'); 		    // 倒计时的秒数

    var Openlistn = $('.js_openlist'); 		        // 收起和展开按钮
    var SelfOpenListBtn = $('.js_receiver_openlist'); 		        // 收起和展开按钮


    module.exports = {
        init: function(data,names) {
            giftSend.giftInit(true);
            giftSend.expressStyle();

            // 倒计时
            oCountDownPart.each(function (i) {
                var times = oCountDownPart.eq(i).attr('data-timer');
                giftSend.giftCountDown(times,giftHour.eq(i),giftMinute.eq(i),giftSecond.eq(i))
            })

            // 页面尺寸修改
            oWindow.on('resize',function() {
                if (resizeTimer) {
                    clearTimeout(resizeTimer)
                }
                resizeTimer = setTimeout(function(){
                    giftSend.giftInit(true);
                    giftSend.expressStyle();
                }, 400);

            });

        }
    };


   /* // 展开收起
    Openlistn.hammer().on('tap', function () {
        var oBtn_Open = $(this).parents('.js_btn_Open');
        var oGift_getListInfo = oBtn_Open.parents('.js_gift_getList').find('.js_gift_getListInfo');
        if( oBtn_Open.hasClass('act-show') ) {
            oBtn_Open.removeClass('act-show');
            oBtn_Open.find('.js_hide').show();
            oBtn_Open.find('.js_show').hide();
            oGift_getListInfo.removeClass('hideHeight');
        }
        else {
            oBtn_Open.addClass('act-show');
            oBtn_Open.find('.js_hide').hide();
            oBtn_Open.find('.js_show').show();
            oGift_getListInfo.addClass('hideHeight');
        }

    });*/

    // 展开收起
    SelfOpenListBtn.hammer().on('tap', function () {
        var oBtn_Open = $(this).parents('.js_btn_Open');
        var oGift_getListInfo = oBtn_Open.parents('.js_gift_getList').find('.js_gift_getListInfo');
        var oGift_getSelfInfo = $('.js_gift_getSelfInfo');
        if( oBtn_Open.hasClass('act-show') ) {
            oGift_getListInfo.show();
            oGift_getSelfInfo.hide();
            oBtn_Open.removeClass('act-show');
            oBtn_Open.find('.js_hide').show();
            oBtn_Open.find('.js_show').hide();
            oGift_getListInfo.removeClass('hideHeight');
        }
        else {
            oGift_getSelfInfo.show();
            oGift_getListInfo.hide();
            oBtn_Open.addClass('act-show');
            oBtn_Open.find('.js_hide').hide();
            oBtn_Open.find('.js_show').show();
            oGift_getListInfo.addClass('hideHeight');
        }
        myPub.HamstopPropaga();
    });



    var giftSend = {
        giftTimer: null,            // 倒计时定时器

        // 页面初始化
        giftInit: function (off) {
            if(off) {
                oContain.css({
                    'padding-top': oHeader.outerHeight(true),
                    'padding-bottom' : oFooter.outerHeight(true)
                });
            }
            oGift_contain.css({
                'min-height': oWindow.height() - oHeader.outerHeight(true)
            })
        },

        //倒计时
        // totalTime:总秒数
        // hourObj:小时放置的对象
        // minuteObj:分钟放置的对象
        // secondObj:秒数放置的对象
        // fn:倒计完成的函数
        giftCountDown: function(totalTime, hourObj, minuteObj, secondObj, fn) {
            var _this = this;
            _this.giftTimer = setInterval(function() {
                var day=0,hour = 0,minute = 0,second = 0; //时间默认值
                if(totalTime > 0) {
                    day = Math.floor(totalTime / (60 * 60 * 24));
                    hour = Math.floor(totalTime / (60 * 60)) - (day * 24);
                    minute = Math.floor(totalTime / 60) - (day * 24 * 60) - (hour * 60);
                    second = Math.floor(totalTime) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
                }else{
                    clearInterval(_this.giftTimer);
                    hourObj.text('00');
                    minuteObj.text('00');
                    secondObj.text('00');
                    if(fn)fn();
                    return;
                }
                if(hour <=9) hour = '0' + hour;
                if(minute <= 9) minute = '0' + minute;
                if(second <= 9) second = '0' + second;

                hourObj.text(hour);
                minuteObj.text( minute);
                secondObj.text(second);
                totalTime--;
            }, 1000);
        },

        // 物流样式
        expressStyle: function () {
            /* 物流样式 */
            var oListInfo = $('.js_listInfo');
            var oLastList = oListInfo.eq(oListInfo.length-1);
            var oFirstList = oListInfo.eq(0);
            var oMarkLine = $('.js_markLine');
            var oMarkTip = $('.js_markTip').eq(oListInfo.length-1);

            var _H = oLastList.outerHeight(true);
            var _disH = _H/2 - oMarkTip.outerHeight(true)*2;

            // 计算线条高度
            oMarkLine.css({
                'bottom':_H - _disH,
                'top': oFirstList.outerHeight(true) - oMarkTip.outerHeight(true)
            });
            // 第一个加标记
            oListInfo.eq(0).addClass('color_org');

            //去除最后一个的底部线条
            oListInfo.eq(oListInfo.length-1).find('.times').css({'border':0})
        }
    }



});



